package com.ruoyi.base.utils.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * redis工具类
 */
@Component
public class RedisUtils {
    protected final StringRedisTemplate redisTemplate;

    @Autowired
    public RedisUtils(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 设置
     */
    public void set(RedisPrefixEnum prefixEnum, String key, String value, Duration duration) {
        redisTemplate.opsForValue().set(getKey(prefixEnum, key), value, duration);
    }

    /**
     * 获取
     */
    public String get(RedisPrefixEnum prefixEnum, String key) {
        return redisTemplate.opsForValue().get(getKey(prefixEnum, key));
    }

    /**
     * 移除
     */
    public void remove(RedisPrefixEnum prefixEnum, String key) {
        redisTemplate.delete(getKey(prefixEnum, key));
    }

    /**
     * 获取key
     */
    private String getKey(RedisPrefixEnum prefixEnum, String key) {
        return prefixEnum.getName() + "_" + key;
    }
}
